<script setup lang="ts">
import { computed, onMounted } from "vue"
import useCover from "../useCover"
import Content from "./content.vue"
import { modelList, priceResizeList, selectValueOptions } from "./type"

const {
  cmdForm,
  resizeId,
  vocabularyList,
  imgCurrentIndex,
  submitLoading,
  imageList,
  handleChangeImg,
  handleChangeResize,
  handleGenerateRandomValue,
  handleSelectChange,
  handleSubmit,
  getImageList
} = useCover()

onMounted(getImageList)

const shouldDisableButton = computed(() => {
  const { command, cmd_proportion, model } = cmdForm.value
  return ![command, cmd_proportion, model].every((value) => value !== "")
})
</script>

<template>
  <div class="desc-info">
    <div class="flex-info">
      <div class="left">描述</div>
      <div class="right-flex" @click="handleGenerateRandomValue">
        <el-icon>
          <Refresh />
        </el-icon>
        随机示例
      </div>
    </div>
    <div class="textarea">
      <el-input :rows="4" v-model="cmdForm.command" type="textarea" placeholder="请输入您所想的描述信息" />
    </div>
  </div>
  <Content>
    <template #title>
      <div class="left">模型</div>
    </template>
    <div class="model-flex">
      <el-button
        type="danger"
        :class="['model-btn', item.model === cmdForm.model ? 'active' : '']"
        v-for="item in modelList"
        :key="item.model"
        @click="cmdForm.model = item.model"
      >
        {{ item.name }}
      </el-button>
    </div>
  </Content>
  <Content>
    <template #title>
      <div class="left">图片比例</div>
    </template>
    <div class="scale">
      <div
        @click="handleChangeResize(item)"
        :class="['item', resizeId === item.id ? 'active' : '']"
        v-for="item in priceResizeList"
        :key="item.id"
      >
        <div class="item-bd" :style="{ width: `${item.width}px`, height: `${item.height}px` }"></div>
        <div class="item-i">{{ item.scale }}</div>
      </div>
    </div>
  </Content>
  <Content class="terms">
    <template #title>
      <div class="left">辅助词条</div>
    </template>
    <div class="me">
      <div class="me-options" v-for="(item, index) in selectValueOptions" :key="index">
        <el-select
          style="width: 100%; margin: 0 3px"
          :placeholder="item.value"
          :model-value="vocabularyList[index]"
          @change="(val) => handleSelectChange(val, index)"
        >
          <el-option
            v-for="optionItem in item.options"
            :key="optionItem.value"
            :label="optionItem.label"
            :value="optionItem.value"
          />
        </el-select>
      </div>
    </div>
  </Content>
  <Content class="terms">
    <template #title>
      <div class="left">绘画风格</div>
    </template>
    <div class="img-info">
      <div
        :class="['img-list', imgCurrentIndex === index ? 'img-active' : '']"
        @click="handleChangeImg(item, index)"
        v-for="(item, index) in imageList"
        :key="index"
      >
        <img :src="item.path" alt="" />
        <div class="name">{{ item.name }}</div>
      </div>
    </div>
  </Content>
  <el-button
    type="danger"
    :loading="submitLoading"
    :disabled="shouldDisableButton"
    @click="handleSubmit"
    class="submit-btn margin"
    >开始绘画</el-button
  >
</template>

<style lang="scss" scoped>
.desc-info {
  .flex-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;

    color: var(--el-color-info);
    font-size: 14px;
    display: flex;
  }
}

.model-flex {
  display: grid;
  align-items: center;
  justify-items: center;
  grid-gap: 12px 12px;
  grid-template-columns: repeat(auto-fit, minmax(70px, 1fr));

  .model-btn {
    width: 100%;
    height: 40px;
    margin: 0 10px;
    background: var(--v3-body-bg-color);
    color: #999;
    border: unset;

    &.active {
      background: rgba(249, 232, 233) 50%;
      color: var(--el-color-primary);
      font-weight: 700;
    }
  }
}

.margin {
  margin-top: 30px;
}

.right-flex {
  display: flex;
  justify-content: center;
  align-items: center;
  color: var(--el-color-primary);
  cursor: pointer;
}

.submit-btn {
  width: 100%;
  height: 40px;
  margin-bottom: 40px;
}

.terms ::v-deep .title {
  margin-top: 20px !important;
}

.scale {
  /* display: grid;
  align-items: center;
  justify-items: center;
  grid-template-columns: repeat(auto-fit, minmax(70px, 2fr)); */
  display: flex;
  flex-wrap: wrap;

  .item {
    width: 70px;
    height: 70px;
    border-radius: 10px;
    background: var(--v3-body-bg-color);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin: 0 12px 12px 0;
    cursor: pointer;

    &-i {
      color: #999;
      font-size: 12px;
      padding-top: 6px;
    }

    &-bd {
      border-radius: 4px;
      border: 1px solid #999;
    }

    &.active {
      background: rgba(249, 232, 233) 50%;

      .item-i {
        color: var(--el-color-primary);
      }

      .item-bd {
        border-color: var(--el-color-primary);
      }
    }
  }
}

.me {
  width: 100%;
  display: grid;
  align-items: center;
  justify-items: center;
  /* grid-gap: 12px 12px; */
  grid-template-columns: 1fr 1fr;

  &-options {
    width: 100%;
    margin-bottom: 12px;

    &:nth-child(odd) {
      margin-right: 12px;
    }
  }
}

.img-info {
  /* display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-items: center; */
  display: grid;
  align-items: center;
  justify-items: center;
  grid-gap: 12px 12px;
  grid-template-columns: repeat(auto-fit, minmax(70px, 1fr));

  .img-list {
    position: relative;
    border-radius: 10px;
    cursor: pointer;
    width: 70px;
    height: 70px;
    box-sizing: border-box;

    img {
      display: block;
      width: 100%;
      height: 100%;
      border-radius: 10px;
      transition: width 0.6s;
      box-sizing: border-box;
    }

    .name {
      width: 100%;
      position: absolute;
      z-index: 9;
      left: 0;
      bottom: 0;
      color: #fff;
      font-size: 10px;
      padding: 3px 0;
      display: flex;
      justify-content: center;
      align-items: center;
      background: rgba(51, 51, 51, 0.6);
      border-radius: 0 0 6px 6px;
      user-select: none;
    }
  }

  .img-active img {
    border: 2px solid var(--el-color-primary);
  }
}
</style>
